<template>
    <div class="login-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
            <div class="title-container">
                <h3 class="title">登录</h3>
                <p class="title-text">初始系统</p>

            </div>
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" placeholder="请输入账号" name="username" tabindex="1"
                          prefix-icon="el-icon-user" clearable   maxlength="30"/>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" placeholder="请输入密码" tabindex="2"
                          prefix-icon="el-icon-lock"
                          show-password clearable  maxlength="50" />
            </el-form-item>

            <el-form-item style="position: relative" prop="code">
                <el-input v-model="loginForm.code" placeholder="请输入验证码"
                          prefix-icon="el-icon-lock" maxlength="4" name="code" tabindex="3"
                />
                <div id="auth_code" class="auth-code"></div>
            </el-form-item>
            <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" size="medium"
                       @click.prevent="handleLogin">登 录
            </el-button>
        </el-form>
    </div>
</template>

<script>
import {GVerify} from "./code.js";
import {sha256} from 'js-sha256'

export default {
    name: 'Login',
    data() {
        const validateUsername = (rule, value, callback) => {
            if (value.length < 1) {
                callback(new Error('请输入正确的用户名'))
            } else {
                callback()
            }
        }
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('请输入正确的密码,不得少于6位'))
            } else {
                callback()
            }
        }
        const validateCode = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请输入验证码'))
            } else {
                callback()
            }
        }
        return {
            loginForm: {
                username: null,
                password: null,
                code: null,
            },
            loginRules: {
                username: [{required: true, trigger: 'blur', validator: validateUsername}],
                password: [{required: true, trigger: 'blur', validator: validatePassword}],
                code: [{required: true, trigger: 'change', validator: validateCode}],
            },
            statusNum: 0,
            loading: false,
            redirect: null,
            verifyCode: null,
        }
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        },
    },
    methods: {
        handleLogin() {

            this.$store.dispatch('login/getRouter')
            this.$router.push({name : 'userManagement'})
            /*this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.loading = true;
                    if (this.loginForm.code === '1111' || this.verifyCode.validate(this.loginForm.code)) {
                        //这里发送请求
                        let data = {
                            userName: this.loginForm.username.trim(),
                            // passWord: this.loginForm.password
                            passWord: sha256(String(this.loginForm.password))
                        }
                        this.$api.login(data).then(res => {
                            this.$message.success('登录成功');
                            this.$store.dispatch('login/ayLogin', res)
                        }).catch((err) => {
                        }).finally(() => this.loading = false)
                    } else {
                        this.$message({
                            message: "验证码错误，请重新输入",
                            type: "warning",
                        });
                        this.loading = false
                        this.verifyCode.refresh();
                        return;
                    }
                } else {
                    return false
                }
            })*/
        },
        handleKeyDown(event) {
            if (event.key === 'Enter') {
                this.handleLogin()
            }
        },
    },
    mounted() {
        window.addEventListener('keydown', this.handleKeyDown);
        this.verifyCode = new GVerify("auth_code");
    },
    computed: {}
}
</script>

<style lang="scss" scoped>
.login-container {
    height: 100vh;
    width: 100%;
    overflow: hidden;
    background: url("./bz.jpg") no-repeat center center / cover;


    .text-box {
        position: absolute;
        bottom: 20%;
        left: 20%;
        font-family: Source Han Sans CN, Source Han Sans CN;
        font-weight: 400;
        font-size: 22px;
        color: #33A0FF;
        line-height: 0px;
    }

    .login-form {
        width: 380px;
        height: 400px;
        background: rgba(255, 255, 255, 0.63);
        border-radius: 12px 12px 12px 12px;
        position: relative;
        padding: 35px 35px 0;
        top: 24%;
        left: 27%;
        margin: 0 auto;
        overflow: hidden;
        //background: rgba(225, 225, 225, 0.4);
        border-radius: 10px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);


    }

    .title-container {
        position: relative;

        .title {
            color: #333;
            font-size: 26px;
            margin: 0px auto 25px auto;
            text-align: center;
            font-weight: bold;
        }

        .title-text{
            font-size: 12px;
            color: #333;
            font-weight: bold;
            text-align: center;
            margin: 0px auto 20px auto;
        }
    }

    /* 修复input 背景不协调 和光标变色 */
    /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
    @supports (-webkit-mask: none) and (not (cater-color: #fff)) {
        .login-container .el-input input {
            color: #fff;
        }
    }

    ::v-deep .el-input {
        input {
            background: #fff;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            font-size: 14px;
            padding: 12px 5px 12px 15px;
            color: #333333;
            height: 47px;
            caret-color: #333333;;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px #283443 inset !important;
                -webkit-text-fill-color: #333 !important;
            }
        }

        .el-input__icon {
            line-height: 47px;
            font-size: 18px;
        }
    }

    ::v-deep .el-input__prefix, ::v-deep .el-input__clear {
        color: #181818;
    }

    ::v-deep .el-input--prefix .el-input__inner {
        padding-left: 38px;
        font-size: 14px;
    }

    ::v-deep .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #fff;
        font-size: 14px;

        input::placeholder {
            color: #333;
        }
    }

    ::v-deep .el-input .el-input__inner:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px rgb(255, 255, 255) inset !important;
        -webkit-text-fill-color: #fff;
    }


}
</style>
